/**   
 *    
 * 项目名称：SmartKungFuService   
 * 类名称：  ResetPasswordServlet
 * 类描述：  忘记密码 
 * 创建人：zhanghl
 * 创建时间：2016年9月26日 上午10:03:57   
 * 修改人：zhanghl   
 * 修改时间：2016年9月26日 上午10:03:57   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientReqPara;
import com.lst.entity.response.ClientResPara;
import com.lst.model.MstClient;
import com.lst.service.MstClientService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: ResetPasswordServlet
 * @Description: 忘记密码
 * @author zhanghl
 * @date 2016年9月26日 上午10:03:57
 *
 */
public class ResetPasswordServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private MstClientService mstClientService = ctx.getBean(MstClientService.class); 

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		logger.info("doPost begin: " + startDate);

		ClientResPara resPara = new ClientResPara();

		try {
			ClientReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				String mobileno = reqPara.getMobileno();
				String password = reqPara.getPassword();				

				Map<String, Object> reqMap = new HashMap<String, Object>();

				reqMap.put("mobileno", mobileno);
				reqMap.put("enabled", true);
				reqMap.put("isDeleted", false);

				List<MstClient> list = mstClientService.queryByList(reqMap,new PageBounds());
				if(null == list||list.size() == 0 ){	
					resPara.setCode(CommCode.M_A000000);
					resPara.setMessage(CommCode.M_C000001);				
				}else{
					
					MstClient mstclient=new MstClient();
					
					mstclient.setMobileno(mobileno);
					mstclient.setPassword(password);
					mstclient.setUpdatedate(startDate);
					mstclient.setUpdateuser(list.get(0).getId());
					mstclient.setId(list.get(0).getId());
										
	                mstClientService.updateByPrimaryKeySelective(mstclient);
					
	                MstClient mc = mstClientService.selectByPrimaryKey(list.get(0).getId());
	                					
					String mobile = mc.getMobileno().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");

					mc.setMobile(mobile);
					
					resPara.setCode(CommCode.M_Y000000);
					resPara.setMessage(CommCode.M_Y000001);
					resPara.setMstClient(mc);	
				}
			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);

			logger.info("ResetPasswordServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

			logger.info("doPost end: " + DateUtil.calLastedTime(startDate));

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public ClientReqPara getReqPara(HttpServletRequest request) {

		ClientReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new ClientReqPara());

			reqPara = (ClientReqPara) baseRequest.clone();

			String mobileno = request.getParameter("mobileno");
			String password = request.getParameter("password");

			logger.info("reqPara mobileno: " + mobileno);
            logger.info("reqPara password: " + password);

			reqPara.setMobileno(mobileno);	
			reqPara.setPassword(password);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		ClientReqPara reReqPara = (ClientReqPara) reqPara;
		ClientResPara reResPara = (ClientResPara) resPara;

	    if (StringUtils.isEmpty(reReqPara.getMobileno())) {
			reResPara.setCode(CommCode.M_A000000);
			reResPara.setMessage(CommCode.M_P000004);
		} else if (StringUtils.isEmpty(reReqPara.getPassword())){		
			reResPara.setCode(CommCode.M_A000000);
			reResPara.setMessage(CommCode.M_C000008);
		} else{
			flag = true;
		}
		return  flag;
	}
}
